package cn.wolfcode.web.controller;

import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequestMapping("/list")
    @RequiredPermission(name = "字典明细页面", expression = "systemDictionaryItem:list")
    public String list(@ModelAttribute("qo") SystemDictionaryItemQueryObject qo, Model model) {
        //查询所有目录
        List<SystemDictionary> systemDictionaries =
                systemDictionaryService.selectSystemDictionaryList();
        model.addAttribute("systemDictionaries", systemDictionaries);

        PageInfo pageInfo = systemDictionaryItemService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "systemDictionaryItem/list";
    }


    @RequestMapping("/delete")
    @RequiredPermission(name = "字典明细删除", expression = "systemDictionaryItem:delete")
    public String delete(Long id) {
        if (id != null) {
            systemDictionaryItemService.deleteSystemDictionaryItem(id);
        }
        return "redirect:/systemDictionaryItem/list";
    }


    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "字典明细新增或修改", expression = "systemDictionaryItem:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem) {
        if (systemDictionaryItem.getId() != null) {
            systemDictionaryItemService.updateSystemDictionaryItem(systemDictionaryItem);
        } else {
            systemDictionaryItemService.insertSystemDictionaryItem(systemDictionaryItem);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setSuccess(true);
        return jsonResult;
    }


    @ResponseBody
    @RequestMapping("/selectByTypeId")
    public List<SystemDictionaryItem> selectByTypeId(Long id) {
        return systemDictionaryItemService.selectByTypeId(id);
    }

    @ResponseBody
    @RequestMapping("/selectByTypeSn")
    public List<SystemDictionaryItem> selectByTypeSn(String sn) {
        return systemDictionaryItemService.selectByTypeSn(sn);
    }


    @ResponseBody
    @RequestMapping("/selectByParentId")
    public List<SystemDictionaryItem> selectByParentId(Long id) {
        return systemDictionaryItemService.selectByParentId(id);
    }
}
